package com.zde.morisocket.controller;

import com.zde.morisocket.pojo.LineUp;
import com.zde.morisocket.pojo.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于接收和广播游戏的数据
 */
@Controller
@Slf4j
public class WebSocketController {

    /**
     * 世界聊天
     * 订阅模式，广播给所有人或指定范围对人
     */
    @MessageMapping("/message")
    @SendTo("/game/message")
    public String position(String msg){
        System.out.println("msg = " + msg);
        return msg;
    }

    @MessageMapping("/officialLiveBroadcast")
    @SendTo("/game/officialLiveBroadcast")
    public String officialLiveBroadcast(String msg){
        return msg;
    }




    /**
     * 测试球球用
     * @param msg
     * @return
     */
    @MessageMapping("/skill")
    @SendTo("/game/skill")
    public String skill(String msg){
        System.out.println("msg = " + msg);
        return msg;
    }

    //消息模板
    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    /**
     * 邀请对方进入组队
     * 1对1发送
     */
    @MessageMapping("/invitation")
    public void invitation(Message message){
        System.out.println("message = " + message);
        //给谁发
        String toUserId = message.getToUserId();
        //接收地址
        String path = "/game/"+toUserId;
        //发送给某人
        messagingTemplate.convertAndSend(path,message);
    }

    //充当队列放用户排队信息
    private static List<LineUp> userList = new ArrayList<>();
    @Autowired
    private SimpMessageSendingOperations messageSending;
    //接收排队的通信信息
    @MessageMapping("/lineUp")
    public void lineUp(LineUp lineUp, SimpMessageHeaderAccessor headerAccessor){
        //同一个用户在会话时只会产生一个唯一id
        String sessionId = headerAccessor.getSessionId();
        lineUp.setSessionId(sessionId);
        //把用户信息放在
        userList.add(lineUp);
        //@Slf4j
        log.info("正在排队{} - {}",sessionId,lineUp);
        log.info("当前排队人数{}",userList.size());
        //6个人开一把
        if(userList.size() >= 6){
            //通知这六个人去挑战到某个房间去
            for (LineUp up : userList) {
                //通知
                String sessionId1 = up.getSessionId();
                /**
                 * 用户id
                 * 订阅地址
                 * 携带参数
                 */
                log.info("通知{}",sessionId1);
                //messageSending.convertAndSendToUser(sessionId1,"/game/start","去9527房间开始游戏");
                messagingTemplate.convertAndSendToUser(sessionId1,"/game/start","去9527房间开始游戏");
            }
            userList = new ArrayList<>();
        }
    }
    @MessageMapping("/unLineUp")
    public void unLineUp(SimpMessageHeaderAccessor headerAccessor){
        //同一个用户在会话时只会产生一个唯一id
        String sessionId = headerAccessor.getSessionId();
        int i = 0;
        for (LineUp lineUp : userList) {
            String sessionId1 = lineUp.getSessionId();
            if(sessionId.equals(sessionId1)){
                log.info("取消排队{}",sessionId);
                log.info("当前排队人数{}",userList.size());
                userList.remove(i);
                break;
            }
            i++;
        }
    }



}
